package com.tanhua.app.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.model.mongo.Voice;
import com.itheima.model.pojo.UserInfo;
import com.itheima.model.vo.ErrorResult;
import com.itheima.model.vo.VoiceVo;
import com.tanhua.app.exception.BusinessException;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VoiceApi;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Service
public class VoiceService {


    @Autowired
    private FastFileStorageClient client;

    @Autowired
    private FdfsWebServer webServer;

    @DubboReference
    private VoiceApi voiceApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static Integer remainingTimes = 10;


    //保存语音到mongoDB
    public void saveVoice(MultipartFile soundFile) throws IOException {
        //1 上传语音文件 自己的FastDFS
        String oriName = soundFile.getOriginalFilename();
        //2 通过语音的名字,获取文件的后缀
        String extName = oriName.substring(oriName.lastIndexOf(".") + 1);

        StorePath path = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), extName, null);

        //获取语音访问url
        String voiceUrl = webServer.getWebServerUrl() + path.getFullPath();
        Voice voice = new Voice();
        voice.setUserId(UserHolder.getUserId());
        voice.setVoiceUrl(voiceUrl);
        voiceApi.save(voice);

        String key = "userId" + UserHolder.getUserId();

         remainingTimes = remainingTimes - 1;
        if (remainingTimes == 0) {
            throw new BusinessException(ErrorResult.error());
        }
        redisTemplate.opsForValue().set("userId" + UserHolder.getUserId(), remainingTimes.toString(), 1, TimeUnit.DAYS);

    }

    //查询语音
    public VoiceVo queryVoice() {
        Voice voice = voiceApi.queryVoice();

        Long userId = voice.getUserId();

        if (userId == UserHolder.getUserId()) {
            throw new BusinessException(ErrorResult.error());
        }

        UserInfo userInfo = userInfoApi.findById(userId);


        remainingTimes = remainingTimes - 1;
        if (remainingTimes == 0) {
            throw new BusinessException(ErrorResult.error());
        }
        redisTemplate.opsForValue().set("queryUserId" + UserHolder.getUserId(), remainingTimes.toString(), 1, TimeUnit.DAYS);
        VoiceVo vo = null;
        //5. 封装vo

        if (userInfo != null) {

            vo = VoiceVo.init(userInfo, voice,remainingTimes);
        }

        voiceApi.delete(voice);

        //返回结果
        return vo;
    }
}
